import grpc
from user_srv.proto import user_pb2, user_pb2_grpc


class UserTest:
    def __init__(self):
        channel = grpc.insecure_channel("127.0.0.1:50051")
        self.stub = user_pb2_grpc.UserStub(channel)

    def user_list(self):
        rsp: user_pb2.UserListResponse = self.stub.GetUserList(user_pb2.PageInfo(pn=2, pSize=2))
        print(rsp.total)
        for user in rsp.data:
            print(user.mobile, user.birthDay)

    def get_user_by_id(self, id):
        rsp: user_pb2.UserInfoResponse = self.stub.GetUserById(user_pb2.IdRequest(id=id))
        print(rsp.mobile)

    def get_user_by_mobile(self, mobile):
        rsp: user_pb2.UserInfoResponse = self.stub.GetUserByMobile(user_pb2.MobileRequest(mobile=mobile))
        print(rsp.nickName)

    def create_user(self, nick_name, password, mobile):
        rsp: user_pb2.UserInfoResponse = self.stub.CreateUser(user_pb2.CreateUseriInfo(mobile=mobile,
                                                                                       passWord=password,
                                                                                       nickName=nick_name
                                                                                       ))

        print(rsp.id)

    def update_user(self,id,nick_name,gender,birthday):
        self.stub.UpdateUser(user_pb2.UpdateUserInfo(
            id=id,
            nickName=nick_name,
            gender=gender,
            birthDay=birthday
        ))
        print("更新成功")


if __name__ == '__main__':
    user = UserTest()
    # user.user_list()
    # user.get_user_by_mobile("18782222228")
    # user.create_user(nick_name="bobby",password="12345",mobile="11234567898")
    user.update_user(2,"18782222299","male",birthday=1627488000)
